<template>
  <MainLayout />
</template>
<script>
import MainLayout from "./basic-layout";
// 配置
import Setting from "@/setting";
// 方法
import { getHeaderName, getMenuSider, getSiderSubmenu } from "@/libs/system";

// 菜单和路由
import menuHeader from '@/menu/header';
import menuSider from '@/menu/sider';
import { frameInRoutes } from '@/router/routes';

export default {
  middleware: 'auth',
  components: { MainLayout },
  created() {
    // 处理路由 得到每一级的路由设置
    this.$store.commit("admin/page/init", frameInRoutes);
    // 设置顶栏菜单
    this.$store.commit("admin/menu/setHeader", menuHeader);
    // 加载用户登录的数据
    this.$store.dispatch("admin/account/load");
    // 初始化全屏监听
    this.$store.dispatch("admin/layout/listenFullscreen");
  },
  watch: {
    // 监听路由 控制侧边栏显示 标记当前顶栏菜单（如需要）
    $route(to, from) {
      console.warn("to,from", to, from,menuSider);
      let path = to.matched[to.matched.length - 1].path;
      if (!Setting.dynamicSiderMenu) {
        let headerName = getHeaderName(path, menuSider);
        if (headerName === null) {
          path = to.path;
          headerName = getHeaderName(path, menuSider);
        }
        // 在 404 时，是没有 headerName 的
        if (headerName !== null) {
          this.$store.commit("admin/menu/setHeaderName", headerName);

          const filterMenuSider = getMenuSider(menuSider, headerName);
          this.$store.commit("admin/menu/setSider", filterMenuSider);
          this.$store.commit("admin/menu/setActivePath", to.path);

          const openNames = getSiderSubmenu(path, menuSider);
          this.$store.commit("admin/menu/setOpenNames", openNames);
        }
      }
      // this.appRouteChange(to, from);
    }
  }
};
</script>